//
//  ACAudienceListViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/4/1.
//

#import "ACAudienceListViewController.h"
#import "ACAnchorMemberListCell.h"

@interface ACAudienceListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,copy)NSString *liveID;
@property(nonatomic,strong)NSArray<TRTCLiveUserInfo *> *userList;
@end

@implementation ACAudienceListViewController
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.estimatedRowHeight = 68;
        NSBundle *curB = [NSBundle BundleWithBundleName:ACLiteAVUISDK targetClass:[self class] ];
        [_tableview registerNib:[UINib nibWithNibName:@"ACAnchorMemberListCell" bundle:curB] forCellReuseIdentifier:@"ACAnchorMemberListCell"];
        _tableview.tableFooterView = [UIView new];
    }
    return _tableview;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-ACSafeAreaBottomHeight);
        make.top.mas_equalTo(0);
    }];
    self.userList = [NSArray array];
}
-(void)setLiveRoom:(TRTCLiveRoom *)liveRoom{
    _liveRoom = liveRoom;
    @weakify(self);
    [_liveRoom getAudienceList:^(int code, NSString * _Nullable message, NSArray<TRTCLiveUserInfo *> * _Nonnull userList) {
        @strongify(self)
        self.userList = userList;
        [self.tableview reloadData];
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.userList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ACAnchorMemberListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACAnchorMemberListCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[ACAnchorMemberListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ACAnchorMemberListCell"];
    }
    TRTCLiveUserInfo *liveUserInfo = self.userList[indexPath.row];
    cell.userInfo = liveUserInfo;
    @weakify(self)
    cell.clickActionHandler = ^(NSInteger index) {
        @strongify(self)
        if (index == 1) {
            // 踢出
            if (self.actionHandler) {
                self.actionHandler(liveUserInfo.userId, index);
            }
        }else if(index == 2){
            // 邀请成为主讲
            [self.liveRoom requestInviteAnchor:liveUserInfo.userId responseCallback:^(BOOL agreed, NSString * _Nullable reason) {
                DDLogInfo(@"---requestInviteAnchor---reason-----%@",reason);
                if (self.actionHandler) {
                    self.actionHandler(liveUserInfo.userId, index);
                }
            }];
        }
    };
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - JXCategoryListContentViewDelegate

/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}

@end
